var r=Object.defineProperty;var a=(s,e,n)=>e in s?r(s,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):s[e]=n;var t=(s,e,n)=>(a(s,typeof e!="symbol"?e+"":e,n),n);import{elementIsExistWithLog as o}from"../generalFunctions.js";var l=(i=>(i[i.Top=0]="Top",i[i.Left=1]="Left",i[i.Bottom=2]="Bottom",i[i.Right=3]="Right",i))(l||{});export var ChangePlane=(i=>(i[i.ToLeft=0]="ToLeft",i[i.ToRight=1]="ToRight",i[i.ToTop=2]="ToTop",i[i.ToBottom=3]="ToBottom",i))(ChangePlane||{});export default class h{constructor(e){t(this,"isSwipedClass");t(this,"isSwipedAreaClass");t(this,"actionOnOpening");t(this,"actionOnClosing");t(this,"touchAreaElement");t(this,"swipeableElement");t(this,"startX",0);t(this,"startY",0);t(this,"startMouseX",0);t(this,"startMouseY",0);t(this,"deltaX",0);t(this,"deltaY",0);t(this,"changePlane");t(this,"currentSide");t(this,"minSwipeWidth");t(this,"minSwipeHeight");t(this,"swipeSensitivity");t(this,"maxWorkWidth");t(this,"isElementSwiped",!1);t(this,"pointerDownHandler",e=>{e.button==0&&(this.startX=0,this.startY=0,this.startMouseX=e.clientX,this.startMouseY=e.clientY,this.swipeableElement.style.userSelect="none",this.touchAreaElement.style.cursor="grabbing",window.addEventListener("pointermove",this.pointerMoveHandler))});t(this,"pointerMoveHandler",e=>{document.documentElement.style.cursor="grabbing",window.addEventListener("pointerup",this.swipeEndHandler),this.swipeMove(e)});t(this,"swipeEndHandler",e=>{this.swipeEnd(this.isElementSwiped,!0)});o("toggleBySwipe",e.selector)&&(this.isSwipedClass=e.isSwipedClass??"isSwiped",this.isSwipedAreaClass=e.isSwipedAreaClass??"isSwiped",this.actionOnOpening=e.actionOnOpening,this.actionOnClosing=e.actionOnClosing,this.touchAreaElement=document.querySelector(e.selector),this.changePlane=e.changePlane,this.swipeableElement=document.getElementById(this.touchAreaElement.getAttribute("for-element")),this.swipeSensitivity=e.swipeSensitivity,this.maxWorkWidth=e.maxWorkWidth??1e5,this.minSwipeWidth=Math.trunc(this.swipeableElement.clientWidth*this.swipeSensitivity),this.minSwipeHeight=Math.trunc(this.swipeableElement.clientHeight*this.swipeSensitivity),this.checkMaxWorkWidth(),window.addEventListener("resize",this.checkMaxWorkWidth.bind(this)))}swipeMove(e){this.changePlane==1||this.changePlane==0?(this.deltaX=Math.trunc(e.clientX-this.startMouseX),this.currentSide=this.deltaX>=0?1:3,this.moveX()):(this.deltaY=Math.trunc(e.clientY-this.startMouseY),this.currentSide=this.deltaY>=0?0:2,this.moveY())}isDeltaMoreThanMinValue(e){return e=Math.abs(e),this.changePlane==1||this.changePlane==0?e>=this.minSwipeWidth:e>=this.minSwipeHeight}swipeEnd(e,n){this.swipeableElement.style.translate="",window.removeEventListener("pointermove",this.pointerMoveHandler),this.swipeableElement.style.userSelect="",this.touchAreaElement.style.cursor="",document.documentElement.style.cursor="",window.removeEventListener("pointerup",this.swipeEndHandler),n||(this.swipeableElement.classList.toggle(this.isSwipedClass),this.touchAreaElement.classList.toggle(this.isSwipedAreaClass),this.isElementSwiped=!this.isElementSwiped,e?(this.swipeableElement.showModal?.(),this.actionOnOpening?.(this.swipeableElement),document.body.style.overflowY="hidden"):(this.swipeableElement.close?.(),this.actionOnClosing?.(this.swipeableElement),document.body.style.overflowY=""))}moveX(e=this.deltaX){if(this.checkSwipeableElementContainActive()){if(!this.isSwipeDirectionCorrectXAxis(!0))return;this.swipeableElement.style.translate=`${e}px ${this.startY}px 0`,this.isDeltaMoreThanMinValue(e)&&this.swipeEnd(!1)}else{if(!this.isSwipeDirectionCorrectXAxis(!1))return;this.swipeableElement.style.translate=`${e}px ${this.startY}px 0`,this.isDeltaMoreThanMinValue(e)&&this.swipeEnd(!0)}}moveY(e=this.deltaY){if(this.checkSwipeableElementContainActive()){if(!this.isSwipeDirectionCorrectYAxis(!0))return;this.swipeableElement.style.translate=`${this.startX}px ${e}px 0`,this.isDeltaMoreThanMinValue(e)&&this.swipeEnd(!1)}else{if(!this.isSwipeDirectionCorrectYAxis(!1))return;this.swipeableElement.style.translate=`${this.startX}px ${e}px 0`,this.isDeltaMoreThanMinValue(e)&&this.swipeEnd(!0)}}checkSwipeableElementContainActive(){return this.swipeableElement.open!=null?this.swipeableElement.open:this.swipeableElement.classList.contains(this.isSwipedClass)}checkMaxWorkWidth(){window.innerWidth<=this.maxWorkWidth?(this.touchAreaElement.addEventListener("pointerdown",this.pointerDownHandler),this.touchAreaElement.style.cursor="",window.addEventListener("pointerup",this.swipeEndHandler)):(this.touchAreaElement.removeEventListener("pointerdown",this.pointerDownHandler),this.touchAreaElement.style.cursor="auto",window.removeEventListener("pointerup",this.swipeEndHandler))}isSwipeDirectionCorrectXAxis(e){if(e){if(this.changePlane==0&&this.currentSide==1||this.changePlane==1&&this.currentSide==3)return!0}else if(this.changePlane==0&&this.currentSide==3||this.changePlane==1&&this.currentSide==1)return!0;return!1}isSwipeDirectionCorrectYAxis(e){if(e){if(this.changePlane==3&&this.currentSide==2||this.changePlane==2&&this.currentSide==0)return!0}else if(this.changePlane==3&&this.currentSide==0||this.changePlane==2&&this.currentSide==2)return!0;return!1}}
